package com.qiang.manager.controller;

import com.qiang.manager.domain.Student;
import com.qiang.manager.factory.Factory;
import com.qiang.manager.service.IstudentSerice;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentControllerImp implements IstudentController {

    Scanner sc = new Scanner(System.in);
    IstudentSerice service = Factory.getStudentService();

    @Override
    public void addStudent() {
        lo:
        while (true) {
            //键盘录入学号
            System.out.println("请输入学生学号");
            String id = sc.next();
            //调用方法，判断id
            boolean result = service.Exit(id);
            if (result) {
                System.out.println("您输入的学号已存在，请重新输入");
                //break;
            } else {
                Student stu = getStudent(id);

                //调用方法
                service.addStudent(stu);
                System.out.println("添加成功");
                break lo;
            }


        }
    }

    @Override
    public void deleteStudent() {
        String id;
        lo:
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();
            boolean result = service.Exit(id);
            //判断学生id是否存在
            if (!result) {
                System.out.println("您输入的id不存在");
                break lo;
            } else {
                service.deleteStudent(id);
                System.out.println("删除成功");
                break lo;
            }
        }



    }

    @Override
    public void updateStudent() {
        String id;
        lo:
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();
            boolean result = service.Exit(id);
            //判断学生id是否存在
            if (!result) {
                System.out.println("您输入的id不存在，请重新输入");
                break lo;
            } else {
                Student newstudent = getStudent(id);

                service.updateStudent(id, newstudent);
                System.out.println("修改成功");
                break lo;
            }
        }


    }

    private Student getStudent(String id) {
        System.out.println("请输入名字");
        String name = sc.next();

        System.out.println("请输入年龄");
        int age = sc.nextInt();

        System.out.println("请输入班级");
        String banji = sc.next();

        //创建学生对象
        Student newstudent = new Student();
        newstudent.setId(id);
        newstudent.setName(name);
        newstudent.setAge(age);
        newstudent.setBanji(banji);
        return newstudent;
    }

    @Override
    public void findStudent() {

        //调用方法
        ArrayList<Student> student = service.findStudent();

        if (student.size() == 0) {
            System.out.println("查无信息，请输入后再查看");
            return;
        }
        System.out.println("id\t\t" + "姓名\t\t" + "年龄\t\t" + "班级");
        for (int i = 0; i < student.size(); i++) {
            if (student.size() != 0) {
                System.out.println(student.get(i).getId() + "\t\t" + student.get(i).getName() + "\t\t" + student.get(i).getAge() + "\t\t" + student.get(i).getBanji() + "\t\t");
            }
        }

    }


    @Override
    public void Start() {
        lo:
        while (true) {
            System.out.println("------欢迎来到学生管理系统------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出系统");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    findStudent();
                    break;
                case 5:
                    break lo;
                default:
                    System.out.println("等大你的双眼看清楚再输入");
                    break;
            }
        }
    }
}
